﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Sublimation
{
    public class SlotEnergy : Slot
    {
        protected List<Tower> destinations;
        protected int transmissionSpeed;
        //protected EnergyRing energyRing;

        public SlotEnergy(WorldProperties worldProperties, SlotKind kind, Tower ownerTower)
            : base(worldProperties, kind, ownerTower)
        {
            transmissionSpeed = 100;
            //energyRing = createEnergyRing();
        }

        public virtual EnergyRing createEnergyRing()
        {
            return new EnergyRing(worldProperties, ownerTower);
        }
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            //energyRing.Rotation = ownerTower.Rotation;
            lookForConnection();
            foreach (Tower destination in destinations)
            {
                //Console.WriteLine("connected " + destination.Energy.Amount);
                if (destination.Energy.Amount < ownerTower.Energy.Amount)
                {
                    if (transmissionSpeed > ownerTower.Energy.Amount)
                    {
                        float temp = (destination.Energy.Amount + ownerTower.Energy.Amount) / 2;
                        destination.Energy.Amount = temp;
                        ownerTower.Energy.Amount = temp;
                    }
                    destination.Energy.Amount += transmissionSpeed;
                    ownerTower.Energy.Amount -= transmissionSpeed;
                }
            }
        }

        protected void lookForConnection()
        {
            destinations = new List<Tower>();
            foreach (Tower tower in worldProperties.Players[0].Towers)
            {
                if (Vector2.Distance(ownerTower.Position, tower.Position) > 0 && Vector2.Distance(ownerTower.Position, tower.Position) < range)
                {
                    //get the angle with the enemy
                    Vector2 direction = tower.Position - ownerTower.Position;
                    direction.Normalize();
                    direction.Y = -direction.Y;
                    float angle_direction;
                    if (direction.Y >= 0)
                    {
                        angle_direction = (float)Math.Acos(direction.X) % MathHelper.TwoPi;
                    }
                    else
                    {
                        angle_direction = -(float)Math.Acos(direction.X) % MathHelper.TwoPi;
                    }
                    //make sure it's between 0 and 2 PI
                    if (angle_direction < 0)
                    {
                        angle_direction += MathHelper.TwoPi;
                    }


                    float aimingAngle = ownerTower.Rotation + orientation;
                    //get the minimum angle
                    float min_angle = ((-aimingAngle - angle)) % MathHelper.TwoPi;
                    //make sure it's between 0 and 2 PI
                    if (min_angle < 0)
                    {
                        min_angle += MathHelper.TwoPi;
                    }

                    //get the maximum angle
                    float max_angle = ((-aimingAngle + angle)) % MathHelper.TwoPi;
                    //get sure it's between 0 and 2 PI
                    if (max_angle < 0)
                    {
                        max_angle += MathHelper.TwoPi;
                    }

                    bool isTarget = false;
                    if (max_angle < min_angle)
                    {
                        if (angle_direction < max_angle || angle_direction > min_angle && angle_direction > max_angle)
                        {
                            isTarget = true;
                        }
                    }
                    else
                    {
                        if (angle_direction < max_angle && angle_direction > min_angle)
                        {
                            isTarget = true;
                        }
                    }
                    //Console.WriteLine(angle_direction + " " + min_angle + " " + max_angle);
                    if (isTarget)
                    {
                        // target and it's closer than the existing one
                        destinations.Add(tower);
                    }
                }
            }
        }

    }
}
